/**
 * Created by liubin on 2015/9/16.
 */
'use strict';

import React from 'react';
import ReactMixin from 'react-mixin';
import Autobind from 'tools/autobindMixin';
import Immutable from 'immutable';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import * as authActionCreators from 'actions/authAction';
import * as userActionCreators from 'actions/userAction';
import * as roleActionCreators from 'actions/roleAction';

@ReactMixin.decorate(Autobind)
class App extends React.Component {
    /**
     * 返回按钮事件
     * 全局设置
     */
    handleBack() {
        const { history } = this.props;
        history.goBack();
    }
    render() {
        let props = Immutable.Map(this.props);
        props = props.set('handleBack', this.handleBack);
        const children = props.get('children');
        props = props.delete('children').toObject();
        return React.cloneElement(children, props);
    }
}

function mapStateToProps(state) {
    return {
        authorization: state.authorization,
        userStore: state.userStore,
        roleStore: state.roleStore
    };
}

function mapDispatchToProps(dispatch) {
    //return {
    //    ...bindActionCreators(Object.assign({}, authActionCreators), dispatch),
    //    dispatch
    //};
    //return Object.assign({}, bindActionCreators(Object.assign({}, authActionCreators), dispatch), { dispatch });
    return {
        authActions: bindActionCreators(authActionCreators, dispatch),
        userActions: bindActionCreators(userActionCreators, dispatch),
        roleActions: bindActionCreators(roleActionCreators, dispatch),
        dispatch
    };
}

export default connect(mapStateToProps, mapDispatchToProps)(App);